/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.apps.utils;

import com.apps.controllers.ShellController;
import com.apps.ui.StatusUI;
import java.awt.Component;
import java.util.ArrayList;
import javax.swing.JPanel;

/**
 *
 * @author ARIJIT PAL
 */
public class StatusChecker implements Runnable{

    private ArrayList<ShellController> shellList;
    private JPanel jPanel;
    private boolean refreshFlag;
    private boolean stopFlag;
    
    public StatusChecker(){
        stopFlag = false;
    }
    
    public boolean isRefreshFlag() {
        return refreshFlag;
    }

    public void setRefreshFlag(boolean refreshFlag) {
        this.refreshFlag = refreshFlag;
    }
    
    private void setAccList(ArrayList<ShellController> shellList){
        this.shellList = shellList;
    }
    public void setUI(JPanel ui){
        jPanel = ui;
    }
    
    public void setStopFlag(boolean flag){
        stopFlag = flag;
    }
    
    public void run(){
        while (!stopFlag) {
            if(refreshFlag){
                refreshFlag = false;
                setAccList(StartupHelper.getShellList());
                jPanel.removeAll();
                jPanel.repaint();
                if(shellList!=null){
                    for (ShellController shell : shellList) {
                        StatusUI statusUI = new StatusUI();
                        statusUI.setShell(shell);
                        statusUI.setAccID(shell.getAccID());
                        statusUI.setHostName(shell.getHost() + ":" + shell.getPort());
                        statusUI.setActiveInd(shell.getActiveFlag());
                        if(shell.getShellStatus()){
                            statusUI.setFlagCL("green");
                        }else{
                            statusUI.setFlagCL("red");
                        }
                        jPanel.add(statusUI);
                    }
                    jPanel.updateUI();
                    jPanel.repaint();
                }
            }else{
                for(Component component : jPanel.getComponents()){
                    
                    StatusUI statusUI = (StatusUI) component;
                    ShellController shell = statusUI.getShell();
                    statusUI.setAccID(shell.getAccID());
                    statusUI.setHostName(shell.getHost() + ":" + shell.getPort());
                    statusUI.setActiveInd(shell.getActiveFlag());
                    if(shell.getShellStatus()){
                        statusUI.setFlagCL("green");
                    }else{
                        statusUI.setFlagCL("red");
                    }
                }
            }
            try {
                Thread.currentThread().sleep(5000);
            } catch (Exception ex) {
            }
        }
    }
}
